package com.myProject.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.myProject.bean.TNote;
import com.myProject.bean.TUser;
import com.myProject.serves.Impl.LogSevImpl;
import com.myProject.serves.Impl.UserSevImpl;
import com.myProject.serves.LogSev;
import com.myProject.serves.UserSev;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "RegisterServlet", urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
    UserSev us = new UserSevImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if ("newUser".equalsIgnoreCase(med)) {
            newUser(request, response);
        }
    }

    private void addUserLog(HttpServletRequest request, HttpServletResponse response) {
        LogSev ls = new LogSevImpl();
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TNote t = new TNote();
        Cookie[] cookies = request.getCookies();
        String user = "";
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("loginName".equals(cookie.getName())) {
                    user = cookie.getValue();
                    System.out.println("user = " + user);
                    t.setNote("用户" + user + "进行了登录操作!");
                    t.setTime(d);
                    ls.addLog(t);
                }
            }
        }
    }

    private void newUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String realName = request.getParameter("realName");
        TUser tUser = new TUser();
        tUser.setUser(username);
        tUser.setPassword(password);
        tUser.setRealname(realName);
        tUser.setLevel(2);
        boolean flag = us.addUser(tUser);
        if (flag) {
            Cookie cookie = new Cookie("loginName", username);
            response.addCookie(cookie);
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString("Hello");
            System.out.println("json = " + json);
            pw.write(json);
            LogSev ls = new LogSevImpl();
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TNote t = new TNote();
            String user = "";
            user = cookie.getValue();
            System.out.println("user = " + user);
            t.setNote("用户" + user + "进行了登录操作!");
            t.setTime(d);
            ls.addLog(t);
        }
    }
}
